//index.js

import rpn from "../../utils/rpn.js";
import myMath from "../../utils/myMath.js";

Page({
    options: {
        // 指定所有 _ 开头的数据字段为纯数据字段
        pureDataPattern: /^_/
    },
    data: {
        dataArr: [
            {id: 0, text: '简单计算器', slotName: 'general'},
            {id: 1, text: '高级计算器', slotName: 'complex'},
            {id: 2, text: '历史记录', slotName: 'history'},
        ],
        /* 简单计算器内容 */
        simplenBtns: [
            {id: 0, text: "存储", class: 'orange'},
            {id: 1, text: "取存", class: 'orange'},
            {id: 2, text: "退格", class: 'orange'},
            {id: 3, text: "清屏", class: 'orange'},
            {id: 4, text: "累存", class: 'orange'},
            {id: 5, text: "积存", class: 'orange'},
            {id: 6, text: "清存", class: 'orange'},
            {id: 7, text: "÷", class: 'brown'},
            {id: 8, text: "7", class: 'gray'},
            {id: 9, text: "8", class: 'gray'},
            {id: 10, text: "9", class: 'gray'},
            {id: 11, text: "×", class: 'brown'},
            {id: 12, text: "4", class: 'gray'},
            {id: 13, text: "5", class: 'gray'},
            {id: 14, text: "6", class: 'gray'},
            {id: 15, text: "-", class: 'brown'},
            {id: 16, text: "1", class: 'gray'},
            {id: 17, text: "2", class: 'gray'},
            {id: 18, text: "3", class: 'gray'},
            {id: 19, text: "+", class: 'brown'},
            {id: 20, text: "0", class: 'gray'},
            {id: 21, text: ".", class: 'gray'},
            {id: 22, text: "", class: 'giveaReward'},
            {id: 23, text: "=", class: 'black'},
        ],
        /*高级计算器的部分内容*/
        complexBtns: [
            {id: 24, text: "存储", class: 'orange', disabled: false},
            {id: 25, text: "取存", class: 'orange', disabled: false},
            {id: 26, text: "累存", class: 'orange', disabled: false},
            {id: 27, text: "积存", class: 'orange', disabled: false},
            {id: 28, text: "清存", class: 'orange', disabled: false},
            {id: 29, text: "7", class: 'gray', disabled: false},
            {id: 30, text: "8", class: 'gray', disabled: false},
            {id: 31, text: "9", class: 'gray', disabled: false},
            {id: 32, text: "÷", class: 'brown', disabled: false},
            {id: 33, text: "取余", class: 'orange', disabled: false},
            {id: 34, text: "4", class: 'gray', disabled: false},
            {id: 35, text: "5", class: 'gray', disabled: false},
            {id: 36, text: "6", class: 'gray', disabled: false},
            {id: 37, text: "×", class: 'brown', disabled: false},
            {id: 38, text: "取整", class: 'orange', disabled: false},
            {id: 39, text: "1", class: 'gray', disabled: false},
            {id: 40, text: "2", class: 'gray', disabled: false},
            {id: 41, text: "3", class: 'gray', disabled: false},
            {id: 42, text: "-", class: 'brown', disabled: false},
            {id: 43, text: "左移", class: 'orange', disabled: false},
            {id: 44, text: "0", class: 'gray', disabled: false},
            {id: 45, text: "+/-", class: 'gray', disabled: false},
            {id: 46, text: ".", class: 'gray', disabled: false},
            {id: 47, text: "+", class: 'brown', disabled: false},
            {id: 48, text: "=", class: 'black', disabled: false},
            {id: 49, text: "与", class: 'purple', disabled: false},
            {id: 50, text: "或", class: 'purple', disabled: false},
            {id: 51, text: "非", class: 'purple', disabled: false},
            {id: 52, text: "异或", class: 'purple', disabled: false},
            {id: 53, text: "A", class: 'blue', disabled: true},
            {id: 54, text: "B", class: 'blue', disabled: true},
            {id: 55, text: "C", class: 'blue', disabled: true},
            {id: 56, text: "D", class: 'blue', disabled: true},
            {id: 57, text: "E", class: 'blue', disabled: true},
            {id: 58, text: "F", class: 'blue', disabled: true},
            {id: 59, text: "PI", class: 'gray', disabled: false},
            {id: 60, text: "E", class: 'gray', disabled: false},
            {id: 61, text: "d.ms", class: 'gray', disabled: false},
            {id: 62, text: "(", class: 'gray', disabled: false},
            {id: 63, text: ")", class: 'gray', disabled: false},
            {id: 64, text: "ln", class: 'gray', disabled: false},
            {id: 65, text: "log", class: 'gray', disabled: false},
            {id: 66, text: "sin", class: 'gray', disabled: false},
            {id: 67, text: "cos", class: 'gray', disabled: false},
            {id: 68, text: "tan", class: 'gray', disabled: false},
            {id: 69, text: "n!", class: 'gray', disabled: false},
            {id: 70, text: "1/x", class: 'gray', disabled: false},
            {id: 71, text: "x^2", class: 'gray', disabled: false},
            {id: 72, text: "x^3", class: 'gray', disabled: false},
            {id: 73, text: "x^y", class: 'gray', disabled: false},
        ],
        radios: [
            {id: 0, text: '十六进制', active: false},
            {id: 1, text: '十进制', active: true},
            {id: 2, text: '八进制', active: false},
            {id: 3, text: '二进制', active: false},
            {id: 4, text: '角度制', active: false},
            {id: 5, text: '弧度制', active: false}
        ],
        // 四目表达式
        expression: '',
        // 存储
        storage: '0'
    },
    onLoad: function () {
    },
    
    // 简单计算器的控制分发
    SimpleControl(e) {
        // 操作符
        let {ope} = e.currentTarget.dataset;
        let {expression} = this.data;
        // 匹配中文
        let zh = /[\u4e00-\u9fa5]+/;
        if (zh.test(ope)) {
            // 点击的按钮为中文
            this.zhOperation(expression, ope)
        } else {
            this.SimpleCompute(expression, ope);
        }
        
    },
    // 复杂计算器的控制分发
    complexControl(e){
        let {id}=e.currentTarget.dataset;
        let {complexBtns}=this.data;
        // 获取当前点击的按钮data数据对象
        let btn_obj=complexBtns.filter(v=>v.id == id);
        btn_obj=btn_obj[0];
        // 排除不可用按钮
        if(btn_obj.disabled) return;
        // 匹配中文
        let zh = /[\u4e00-\u9fa5]+/;
        if(zh.test(btn_obj.text)){
            // 中文
            return;
        }
        
        console.log(btn_obj,id)
    },
    // 上档功能, 双曲函数
    extendedCheckBox(e){
        let {value}=e.detail;
        let {complexBtns}=this.data;
        // id 对应真实按钮id
        let shiftUp_arr=[
            {id: 38, newText: "小数",oldText:"取整"},
            {id: 61, newText: "deg", oldText: "d.ms"},
            {id: 64, newText:"exp" ,oldText: "ln"},
            {id: 65, newText:"expd",oldText: "log"},
            {id: 65, newText:"expd",oldText: "log"},
            {id: 66, newText:"asin", oldText: "sin", ishyperbolic:true, hyperText:"sinh",hyperUpText:"asinh"},
            {id: 67, newText:"acos", oldText: "cos", ishyperbolic:true, hyperText:"cosh",hyperUpText:"acosh"},
            {id: 68, newText:"atan", oldText: "tan", ishyperbolic:true, hyperText:"tanh",hyperUpText:"atanh"},
            {id: 71, newText:"x^.2", oldText: "x^2"},
            {id: 72, newText:"x^.3", oldText: "x^3"},
            ];
        let up_bool=value.includes('shiftUp'); // 是否选中 上档功能
        let hy_bool=value.includes('hyperbolic'); // 是否选中 双曲函数
        complexBtns.forEach(v=>{
            shiftUp_arr.forEach(i=>{
                if (v.id == i.id){
                    v.text=up_bool?i.newText:i.oldText
                }
                if(v.id == i.id && hy_bool && i.ishyperbolic){
                    // 双曲函数
                    v.text=up_bool?i.hyperUpText:i.hyperText;
                }
            })
        })
        this.setData({complexBtns})
    },
    // 进制改变
    radioChange(e){
        let {id}=e.currentTarget.dataset;
        let {radios,complexBtns}=this.data;
        // 获取当前点击的按钮data数据对象
        let radio_obj;
        for(let item of radios){
            if(item.id==id){
                radio_obj=item;
                item.active=true;
            }else {
                item.active=false;
            }
        }
        // 需要禁用的按钮 id
        let disabledIds=[];
        let a_f=[53,54,55,56,57,58];  // A-F
        let pi_tan=[59,60,61,64,65,66,67,68,69,70,71,72,73]; // pi e d.ms sin..tan等
        let to_serve=[29,30,31,34,35,36,40,41]; // 2-9
        switch (radio_obj.text) {
            case "十六进制":disabledIds=[46,...pi_tan];break;
            case "十进制":disabledIds=a_f;break;
            case "八进制":disabledIds=[30,31,46,...a_f,...pi_tan];break;
            case "二进制":disabledIds=[46,...a_f,...pi_tan,...to_serve];break;
        }
        complexBtns.forEach(v=>v.disabled=disabledIds.includes(v.id)?true:false);
        this.setData({radios,complexBtns})
    },
    // 点击中文按钮的操作
    zhOperation(expression, ope) {
        let {storage} = this.data;
        if (/[\.|\+|\-|×|÷]$/.test(expression)) {
            // 去除最后一个字符为特殊字符
            expression = expression.substring(0, expression.length - 1);
        }
        if (ope == '存储') {
            // 覆盖存储
            storage = expression;
        }
        if (ope == '累存' || ope == '积存') {
            let result1 = new rpn(expression).$calCommonExp();
            let result2 = new rpn(storage).$calCommonExp();
            storage = ope == '累存' ? result1 + result2 + '' : result1 * result2 + '';
        }
        if (ope == '取存') {
            expression = storage;
        }
        if (ope == '清存') {
            storage = '0'
        }
        if (ope == '清屏') {
            expression = '';
        }
        if (ope == '退格') {
            expression = expression.substring(0, expression.length - 1);
        }
        this.setData({storage, expression})
    },
    // 简单计算, 如str: 1+2×6
    SimpleCompute(expression, ope) {
        // 合法的四目运算符
        let _opeReg = new RegExp(/^[\.|\+|\-|×|÷]$/);
        // 防止 . +-×÷出现连线两次
        let lastChar = expression.charAt(expression.length - 1);
        if (_opeReg.test(ope) && _opeReg.test(lastChar)) {
            // 特殊符号
            expression = expression.substring(0, expression.length - 1) + ope;
        } else if (ope == '=' && _opeReg.test(lastChar)) {
            // 去除最后一个字符为特殊字符
            expression = expression.substring(0, expression.length - 1);
        } else {
            expression = ope == '=' ? expression : expression + ope;
        }
        if (ope == '=' && expression != '') {
            // 开始计算 使用 rpn库运算
            let result = new rpn(expression).$calCommonExp();
            expression = result + '';
        }
        this.setData({expression});
    }
})
